package com.example.service;

import com.example.entity.Good;
import com.example.entity.GoodStock;
import com.example.entity.Params;
import com.example.mapper.GoodStockMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodStockService {
    @Autowired
    private GoodStockMapper goodStockMapper;
    @Autowired
    private GoodService goodService;


    public List<GoodStock> findAll() {
        return goodStockMapper.selectAll();
    }

    public PageInfo<Good> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());


        List<Good> list = goodStockMapper.findBySearch(params);


        return PageInfo.of(list);
    }

    @Transactional
    public void add(GoodStock goodStock) {

        Good good = goodService.selectById(goodStock.getGoodid());
        Integer num = goodStock.getNum();
        good.setNum(good.getNum() + num);
        goodService.updateById(good);


        goodStockMapper.add(goodStock);
    }

    public void delete(Integer id) {
        goodStockMapper.deleteById(id);
    }

}
